<!DOCTYPE html>
<html>
<head>
    <title>分类管理</title>
    <#include "/header.html">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div class="layui-btn-container">
            <button class="layui-btn" id="btn-expand">全部展开</button>
            <button class="layui-btn layui-bg-black" id="btn-fold">全部折叠</button>
            <#if shiro.hasPermission("app:category:save")>
            <button class="layui-btn layui-btn-normal" id="btn-add"><i class="layui-icon layui-icon-add-circle"></i>添加分类</button>
            </#if>
        </div>
        <div style="margin: 10px 0">
            <form class="layui-form layui-form-pane" action="" id="searchForm">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">分类</label>
                        <div class="layui-input-inline" id="positionListBox">
                        	<input type="text" name="name" placeholder="搜索分类" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn layui-btn-primary layui-border-blue" lay-submit lay-filter="form-search-btn"><i class="layui-icon layui-icon-search"></i>搜索</button>
                        <button class="layui-btn layui-btn-primary" lay-submit lay-filter="form-reset-btn"><i class="layui-icon layui-icon-refresh"></i>重置</button>
                    </div>
                </div>
            </form>
        </div>
        <table class="layui-table" id="currentTableId"></table>
    </div>
</div>

<script type="text/html" id="currentTableTagTpl">
    {{# layui.each(d.keyword, function(index, item){ }}
    <span class="layui-badge-rim">{{item}}</span>
    {{# }); }}
</script>

<script type="text/html" id="currentTableStatusTpl">
    {{# if(d.status == 1){ }}
    <span class="layui-badge layui-bg-green">显示</span>
    {{# } else { }}
    <span class="layui-badge layui-bg-grey">隐藏</span>
    {{# } }}
</script>

<script type="text/html" id="imgtemp">
	<div><a href="{{d.image}}" target="_blank"><img src="{{d.image}}" class="layui-nav-img" style="width:20px;height:20px"></a></div>
</script>
<script type="text/html" id="currentTableBar">
    {{# if(d.id !== 1){ }}
    <#if shiro.hasPermission("app:category:save")>
    <a class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
    </#if>
    <#if shiro.hasPermission("app:category:delete")>
    <a class="layui-btn layui-btn-primary layui-border-red layui-btn-xs" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</a>
    </#if>
    {{# } }}
</script>

<script type="text/javascript">
    var formDataId = 0;
    var menuTreeData = [];
    var table;

    function loadMenuData() {
        menuTreeData = [];
        $.ajax({
            type: "POST",
            url: serverUrl('portalcategory/tree'),
            data: {},
            dataType: "json",
            success: function (result) {
                menuTreeData = result;
            }
        });
    }

    $(document).ready(function () {
        loadMenuData();
    });

    layui.use(['form', 'table','treeTable'], function () {
        var $ = layui.jquery,
       		 form = layui.form,
             table = layui.table,
            treeTable = layui.treeTable;

        layer.load(2);
        // 渲染树形表格
        function tableSearch(data,url) {
        	
        	var tablethis = table.render({
                elem: '#currentTableId',
			    where:data,
                url: serverUrl(url),
                toolbar: '#toolbarDemo',
                defaultToolbar: ['filter'],
                cols: [[
                    {type: "numbers", width: 50},
                    {field: 'description', minWidth: 120, title: '描述'},
                    {field: 'seoTitle', minWidth: 120, title: 'seo标题'},
                    {field: 'seoKeywords', minWidth: 120, title: 'seo关键字'},
                    {field: 'seoDescription', minWidth: 120, title: 'seo描述'},
                    {title: '操作', width: 150, toolbar: '#currentTableBar', align: "center"}

                ]],
                limits: [15, 30, 50],
                limit: 15,
                page: true
            });
        	return tablethis;
        };
        table = treeTable.render({
            elem: '#currentTableId',
            url: serverUrl('/portalcategory/list'),
            tree: {
                iconIndex: 2,           // 折叠图标显示在第几列
                isPidData: true,        // 是否是id、pid形式数据
                idName: 'id',  // id字段名称
                pidName: 'parentId'     // pid字段名称
            },
            cols: [[
                {type: 'numbers', width: 80},
                {field: 'listOrder', minWidth: 150, title: '排序值'},
                {field: 'name', minWidth: 150, title: '分类名称'},
                {
                    field: 'grade', width: 80, align: 'center', templet: function (d) {
                        if (d.path.length <5) {
                            return '<span class="layui-badge layui-bg-gray">顶级</span>';
                        }
                        if (d.path.length >5) {
                            return '<span class="layui-badge-rim">一级</span>';
                        }
                        if (d.grade === 2) {
                            return '<span class="layui-badge-rim">二级</span>';
                        }
                        if (d.grade === 3) {
                            return '<span class="layui-badge-rim">三级</span>';
                        }
                    },
                    title: '层级'
                },
                {field: 'description', minWidth: 120, title: '描述'},
                {field: 'seoTitle', minWidth: 120, title: 'seo标题'},
                {field: 'seoKeywords', minWidth: 120, title: 'seo关键字'},
                {field: 'seoDescription', minWidth: 120, title: 'seo描述'},
                /* {field: 'createTime', minWidth: 120, title: '创建时间'}, */
                {field: 'status', title: '状态', minWidth: 80, templet: '#currentTableStatusTpl'},
                {templet: '#currentTableBar', width: 150, align: 'center', title: '操作'}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });

        //添加按钮
        $('#btn-add').click(function () {
            formDataId = 0;
            openFormView('app/hcportalcategory/form.html', '添加分类');
        });
        //展开按钮
        $('#btn-expand').click(function () {
            table.expandAll();
        });
        //折叠按钮
        $('#btn-fold').click(function () {
            table.foldAll();
        });

        treeTable.on('tool(currentTableId)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                formDataId = data.id;
                openFormView('app/hcportalcategory/form.html', '编辑分类');
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除 '+data.name+' 吗？', function (index) {
                    deleteTableRow('portalcategory/delete/', [data.id], function (rs) {
                        table.reload();
                    });
                    layer.close(index);
                });
            }
        });
     // 监听搜索操作
        form.on('submit(form-search-btn)', function (data) {
            //执行搜索重载
            if (data.field.name) {
            	tableSearch({"name":data.field.name},'portalcategory/search');
            //table.filterData(data.field.title);
	        } else {
	            table.clearFilter();
	        }
	        return false;
	    });

    });
</script>

</body>
</html>